package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Usuario;


public class UsuarioDAO implements IUsuarioDAO {
	private static final Log log = LogFactory.getLog(UsuarioDAO.class);

	// property constants
	// public static final Integer USUAACTIVON = "usuaactivon";
	public static final String USUAACTIVON = "usuaactivon";

	// public static final String USUACLAVEV = "usuaclavev";
	public static final String USUACLAVEV = "usuaclavev";

	// public static final String USUACORREOV = "usuacorreov";
	public static final String USUACORREOV = "usuacorreov";

	// public static final Date USUAFECHAACTIVOD = "usuafechaactivod";
	public static final String USUAFECHAACTIVOD = "usuafechaactivod";

	// public static final Integer USUAIDN = "usuaidn";
	public static final String USUAIDN = "usuaidn";

	// public static final String USUAIMAGENT = "usuaimagent";
	public static final String USUAIMAGENT = "usuaimagent";

	// public static final String USUANOMBREV = "usuanombrev";
	public static final String USUANOMBREV = "usuanombrev";

	private Session getSession() {
		return HibernateSessionFactory.getSession();
	}

	/**
	 * 
	 * @param Instance
	 *            Usuario Instance to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Usuario instance) {
		log.debug("saving Usuario instance");

		try {
			getSession().save(instance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * @param Instance
	 *            Usuario Instance to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Usuario instance) {
		log.debug("deleting Usuario instance");

		try {
			getSession().delete(instance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 
	 * @param Instance
	 *            Usuario Instance to update
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void update(Usuario instance) {
		log.debug("updating Usuario instance");

		try {
			getSession().update(instance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public Usuario findById(Integer id) {
		log.debug("finding Usuario instance with id: " + id);

		try {
			Usuario instance = (Usuario) getSession().get(Usuario.class, id);

			return instance;
		} catch (RuntimeException re) {
			log.error("finding Usuario failed", re);
			throw re;
		}
	}

	public List<Usuario> findByExample(Usuario instance) {
		log.debug("finding Usuario instance by example");

		try {
			List results = getSession().createCriteria("Usuario")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());

			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * Find all Usuario entities with a specific property value.
	 * 
	 * @param value
	 *            the property value to match
	 * @param propertyName
	 *            the property to search in the instance
	 * @return List< Usuario> found by query
	 */
	public List<Usuario> findByProperty(String propertyName, Object value) {
		log.debug("finding Usuario instance with property: " + propertyName
				+ ", value: " + value);

		try {
			String queryString = "from Usuario as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Usuario> findByUsuaactivon(Object usuaactivon) {
		return findByProperty(USUAACTIVON, usuaactivon);
	}

	public List<Usuario> findByUsuaclavev(Object usuaclavev) {
		return findByProperty(USUACLAVEV, usuaclavev);
	}

	public List<Usuario> findByUsuacorreov(Object usuacorreov) {
		return findByProperty(USUACORREOV, usuacorreov);
	}

	public List<Usuario> findByUsuafechaactivod(Object usuafechaactivod) {
		return findByProperty(USUAFECHAACTIVOD, usuafechaactivod);
	}

	public List<Usuario> findByUsuaidn(Object usuaidn) {
		return findByProperty(USUAIDN, usuaidn);
	}

	public List<Usuario> findByUsuaimagent(Object usuaimagent) {
		return findByProperty(USUAIMAGENT, usuaimagent);
	}

	public List<Usuario> findByUsuanombrev(Object usuanombrev) {
		return findByProperty(USUANOMBREV, usuanombrev);
	}

	/**
	 * Find all Usuario entities.
	 * 
	 * @return List<Usuario> all Usuario instances
	 */
	public List<Usuario> findAll() {
		log.debug("finding all Usuario instances");

		try {
			String queryString = "from Usuario";
			Query queryObject = getSession().createQuery(queryString);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<Usuario> findByCriteria(String whereCondition) {
		log.debug("finding Usuario " + whereCondition);

		try {
			String where = ((whereCondition == null) || (whereCondition
					.length() == 0)) ? "" : ("where " + whereCondition);
			final String queryString = "select model from Usuario model "
					+ where;
			Query query = getSession().createQuery(queryString);
			List<Usuario> entitiesList = query.list();

			return entitiesList;
		} catch (RuntimeException re) {
			log.error("find By Criteria in Usuario failed", re);
			throw re;
		}
	}

	public List<Usuario> findPageUsuario(String sortColumnName,
			boolean sortAscending, int startRow, int maxResults) {
		log.debug("finding Usuario findPageUsuario");

		if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
			try {
				String queryString = "select model from Usuario model order by model."
						+ sortColumnName
						+ " "
						+ (sortAscending ? "asc" : "desc");

				return getSession().createQuery(queryString)
						.setFirstResult(startRow).setMaxResults(maxResults)
						.list();
			} catch (RuntimeException re) {
				throw re;
			}
		} else {
			try {
				String queryString = "select model from Usuario model";

				return getSession().createQuery(queryString)
						.setFirstResult(startRow).setMaxResults(maxResults)
						.list();
			} catch (RuntimeException re) {
				throw re;
			}
		}
	}

	public Long findTotalNumberUsuario() {
		log.debug("finding Usuario count");

		try {
			String queryString = "select count(*) from Usuario model";
			Query queryObject = getSession().createQuery(queryString);

			return (Long) queryObject.list().get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	

	public List<Usuario> findUsuarioLogin(String usuario, String contrasena) {
		log.debug("finding Usuario login");
		
		try {
			String queryString = "from Usuario as model where model.usuacorreov = ? and model.usuaclavev=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, usuario);
			queryObject.setParameter(1, contrasena);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
		
		
	}
}
